package cn.com.service.impl.sys;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.commons.Paginator;
import cn.com.commons.utils.NumberUtil;
import cn.com.dal.dao.sys.MenuDAO;
import cn.com.dal.dataobject.sys.MenuDO;
import cn.com.service.sys.MenuService;
import static cn.com.support.message.MessageCodeEnum.*;
import cn.com.support.context.MenuContext;
import cn.com.support.dal.DAOException;
import cn.com.support.message.MessageResult;

/**
 * MenuService实现类
 * 
 * @since 2012-3-19 下午9:52:59
 * @version 1.0
 * @author Microbun
 * 
 */
@Service
public class MenuServiceImpl implements MenuService {

	private Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);

	@Autowired
	private MenuDAO<MenuDO> menuDAO;

	@Override
	public MessageResult<Long> insert(MenuDO obj) {
		MessageResult<Long> result = insertToDB(obj);
		// 清空菜单缓存
		resetCache();
		return result;
	}

	/**
	 * 将菜单插入数据库
	 * 
	 * @param obj
	 *            要插入的差点
	 * @return
	 */
	private MessageResult<Long> insertToDB(MenuDO obj) {
		MessageResult<Long> result = new MessageResult<Long>();
		Long id = null;
		try {
			id = menuDAO.insert(obj);
			if (NumberUtil.isPositive(id)) {
				result.setMessageResult(true, id, INFO_SUCCEED);
			}
		} catch (DAOException e) {
			logger.error(String.format(ERROR_INSERT.descript() + ":[%d]", id),
					e);
			result.setMessageResult(false, id, ERROR_INSERT);
		}
		return result;
	}

	@Override
	public MessageResult<Integer> updateById(MenuDO obj) {
		MessageResult<Integer> result = updateByIdToDB(obj);
		// 清空菜单缓存
		resetCache();
		return result;
	}

	/**
	 * 更新数据中的菜单
	 * 
	 * @param obj
	 *            要更新的菜单
	 * @return
	 */
	private MessageResult<Integer> updateByIdToDB(MenuDO obj) {
		MessageResult<Integer> result = new MessageResult<Integer>();
		Integer count = null;
		try {
			count = menuDAO.updateById(obj);
			if (NumberUtil.isPositive(count)) {
				result.setMessageResult(true, count, INFO_SUCCEED);
			}
		} catch (DAOException e) {
			logger.error(String.format(ERROR_UPDATE.descript() + ":[%s]", obj),
					e);
			result.setMessageResult(false, count, ERROR_UPDATE);
		}
		return result;
	}

	/**
	 * 查询菜单类别
	 * 
	 * @param condition
	 * @return
	 */
	private <C> List<MenuDO> selectList(C condition) {
		try {
			return menuDAO.selectList(condition);
		} catch (DAOException e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]", condition),
					e);
		}
		return null;
	}

	@Override
	public MessageResult<Integer> deleteById(Long id) {
		MessageResult<Integer> result = new MessageResult<Integer>();
		Integer count = null;
		try {
			count = menuDAO.deleteById(id);
			if (NumberUtil.isPositive(count)) {
				result.setMessageResult(true, count, INFO_SUCCEED);
			}
		} catch (DAOException e) {
			logger.error(String.format(ERROR_DELETE.descript() + ":[%d]", id),
					e);
			result.setMessageResult(false, count, ERROR_DELETE);
		}
		return result;
	}

	@Override
	public MessageResult<MenuDO> selectById(Long id) {
		MessageResult<MenuDO> result = new MessageResult<MenuDO>();
		MenuDO menuDO = null;
		try {
			menuDO = menuDAO.selectById(id);
			result.setMessageResult(true, menuDO, INFO_SUCCEED);
		} catch (DAOException e) {
			logger.error(String.format(ERROR_SELECT.descript() + ":[%d]", id),
					e);
			result.setMessageResult(false, menuDO, ERROR_DELETE);
		}
		return result;
	}

	@Override
	public <C> Paginator<C, List<MenuDO>> selectForPage(
			Paginator<C, List<MenuDO>> paginator) {
		Paginator<C, List<MenuDO>> result = null;
		try {
			result = menuDAO.selectForPage(paginator);
		} catch (DAOException e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]",
							paginator.getCondition()), e);
		}
		return result;
	}

	@Override
	public void resetCache() {
		MenuContext.getInstance().putAllMenus(selectList(null));
	}

}
